//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by yhlun on 15/11/17.
//  Copyright © 2015年 yhlun. All rights reserved.
//

import UIKit

private var imageCount = 4
private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    
    init() {
    
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

  
    
  


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
        
        cell.index = indexPath.item
    
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / screenW)
        
        if page == imageCount - 1 {
        
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell {
            
                cell.startAnimation()
            }
        }
    }

   
}

class NewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0 {
    
        didSet{
        
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
            
        }
    }
    
    //MARK: 按钮的点击事件
    @objc private func start() {
    
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    
    private func startAnimation() {
    
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.hidden = false
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            
            }) { (_) -> Void in
                
                print("OK")
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2.设置UI，布局
    func setupUI() {
    
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
        //设置布局
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
    }
    
    //1.实例化子视图控件
    
    private lazy var iconImage:UIImageView = UIImageView()
    //开始按钮
    private lazy var startButton:UIButton = {
    
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.hidden = true
        return btn
    }()
}











